package com.xrui.hbase.codec.impl;

import com.google.common.base.Preconditions;

import com.xrui.hbase.DecodedCell;
import com.xrui.hbase.codec.CellEncoder;
import com.xrui.hbase.schema.SchemaType;
import com.xrui.hbase.schema.spec.CellSpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Encoder for cells that expose raw bytes to the user.
 * <p>
 * <p> Gives full control to the user about how a cell is encoded. </p>
 */
public final class RawBytesCellEncoder implements CellEncoder {
    public static final String PROVIDER_NAME = "RawBytesCellEncoder";
    private static final Logger LOG = LoggerFactory.getLogger(RawBytesCellEncoder.class);

    /**
     * Initializes a new RawBytesCellEncoder.
     */
    public RawBytesCellEncoder() throws IOException {
    }

    /**
     * Initializes a new RawBytesCellEncoder.
     *
     * @param cellSpec Specification of the cell to encode.
     * @throws IOException on I/O error.
     */
    public RawBytesCellEncoder(final CellSpec cellSpec) throws IOException {
        Preconditions.checkArgument(cellSpec.getCellSchema().getType() == SchemaType.RAW_BYTES);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public byte[] encode(final DecodedCell<?> cell) throws IOException {
        return encode(cell.getData());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public synchronized <T> byte[] encode(final T cellValue) throws IOException {
        return (byte[]) cellValue;
    }

    @Override
    public String getName() {
        return PROVIDER_NAME;
    }
}
